﻿using System.Linq;

namespace MSHelpMe.ViewModels
{
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;


    public class ScanningState : PhotoCaptureState
    {

        public ScanningState(PhotoCapture ui)
            : base(ui)
        {

        }

        public override void ShowHideControls()
        {
            ui.Dispatcher.BeginInvoke(
                () =>
                {
                    ui.words.Children.Clear();

                    ui.txtError.Visibility = Visibility.Collapsed;

                    ui.viewfinder.Visibility = ui.focusBrackets.Visibility = Visibility.Collapsed;
                    ui.btnShutter.Visibility = Visibility.Collapsed;

                    ui.imgPreview.Visibility = ui.words.Visibility = Visibility.Visible;
                    ui.txtTip.Visibility = Visibility.Visible;
                    ui.scanProgress.Visibility = Visibility.Visible;
                    ui.scanProgress.IsIndeterminate = true;
                    ui.btnAccept.Visibility = Visibility.Collapsed;
                    ui.btnCapture.Visibility = Visibility.Visible;
                });
        }

        public override void FocusClick()
        {
            // Do nothing
        }

        public override void ShutterClick()
        {
            // Do nothing
        }

        public override void RecaptureClick()
        {
            this.ui.ChangeState(new CapturingState(this.ui));
        }

        public override bool AcceptClick()
        {
            var sb = new StringBuilder();

            foreach (var w in this.ui.words.Children.OfType<TextBlock>())
            {
                sb.Append(w.Text);
                sb.Append(" ");
            }

            App.ViewModel.QueryText = sb.ToString().Trim();

            return true;
        }
    }
}
